package com.opencloud.iot.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.opencloud.common.model.PageParams;
import com.opencloud.iot.client.model.entity.IotRecorders;
import com.opencloud.iot.provider.mapper.IotRecordersMapper;
import com.opencloud.iot.provider.service.IotRecordersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.logging.LoggingPermission;

/**
 * 进出记录操作
 * @author yeyong
 */

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class IotRecordersServiceImpl implements IotRecordersService {

    @Autowired
    private IotRecordersMapper iotRecordersMapper;

    @Override
    public IPage<IotRecorders> findListPage(PageParams pageParams, Long StartTime, Long EndTime) {
        IotRecorders query = pageParams.mapToObject(IotRecorders.class);
        QueryWrapper<IotRecorders> queryWrapper = new QueryWrapper();
        queryWrapper.lambda()
                .eq(ObjectUtils.isNotEmpty(query.getWatchmac()),IotRecorders::getWatchmac,query.getWatchmac())
                .eq(ObjectUtils.isNotEmpty(query.getMacaddress()),IotRecorders::getMacaddress,query.getMacaddress())
                .orderByDesc(IotRecorders::getTimestamp);
        queryWrapper.lambda() .between(ObjectUtils.isNotEmpty(StartTime) && ObjectUtils.isNotEmpty(EndTime),IotRecorders::getTimestamp, StartTime+28800, EndTime+28800);
        return iotRecordersMapper.selectPage(new Page(pageParams.getPage(),pageParams.getLimit()),queryWrapper);
    }

    @Override
    public List<IotRecorders> findList(Long StartTime, Long EndTime,String Watchs) {
        //log.error(Watchs);
        QueryWrapper<IotRecorders> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().inSql(ObjectUtils.isNotEmpty(Watchs),IotRecorders::getWatchmac,Watchs);
        queryWrapper.lambda() .between(ObjectUtils.isNotEmpty(StartTime) && ObjectUtils.isNotEmpty(EndTime),IotRecorders::getTimestamp, StartTime+28800, EndTime+28800);
        queryWrapper.lambda().orderByDesc(IotRecorders::getTimestamp);
        return iotRecordersMapper.selectList(queryWrapper);
    }
}
